package com.cnb.telepathytalk.fragment;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cnb.telepathytalk.R;
import com.cnb.telepathytalk.TelepathyTalkApplication;
import com.cnb.telepathytalk.util.PhoneState;

public class RegPhoneInputFragment extends Fragment implements OnClickListener {
	private Context mContext;

	private TextView country;
	private TextView countryNum;
	private TextView phoneNum;
	private View check1;
	private View check2;
	private View accept;

	private String[] countries;
	private String[] countryNums;

	@Override
	public void onAttach(Activity activity) {

		super.onAttach(activity);
		mContext = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.fragment_reg_phone_input, null);
		((TextView) view.findViewById(R.id.titlebarText)).setText(R.string.title_phone_input);

		countries = mContext.getResources().getStringArray(R.array.reg_country_array);
		countryNums = mContext.getResources().getStringArray(R.array.reg_country_num_array);

		country = (TextView) view.findViewById(R.id.phone_input_country);
		countryNum = (TextView) view.findViewById(R.id.phone_input_country_num);
		phoneNum = (TextView) view.findViewById(R.id.phone_input_num);
		check1 = view.findViewById(R.id.phone_input_check1);
		check2 = view.findViewById(R.id.phone_input_check2);
		accept = view.findViewById(R.id.phone_input_accept);

		country.setOnClickListener(this);
		check1.setOnClickListener(this);
		check2.setOnClickListener(this);
		accept.setOnClickListener(this);

		setCountry(0);
		phoneNum.setText(PhoneState.getPhoneNum(mContext));

		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.phone_input_country:
			new AlertDialog.Builder(mContext).setItems(R.array.reg_country_array, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					setCountry(which);
				}
			}).show();
			break;

		case R.id.phone_input_check1:
			new AlertDialog.Builder(mContext).setTitle(R.string.reg_check1).setMessage(readAssetsFile("telepathy_text1.txt"))
					.setPositiveButton(R.string.reg_accept, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							check1.setSelected(true);
						}
					}).setNegativeButton(R.string.reg_cancel, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							check1.setSelected(false);
						}
					}).show();
			break;

		case R.id.phone_input_check2:
			new AlertDialog.Builder(mContext).setTitle(R.string.reg_check2).setMessage(readAssetsFile("telepathy_text2.txt"))
					.setPositiveButton(R.string.reg_accept, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							check2.setSelected(true);
						}
					}).setNegativeButton(R.string.reg_cancel, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							check2.setSelected(false);
						}
					}).show();
			break;

		case R.id.phone_input_accept:
			if (check1.isSelected() && check2.isSelected()) {
				getFragmentManager().beginTransaction().replace(R.id.fragment, new RegNumInputFragment()).commit();
			} else {
				TelepathyTalkApplication.showToast(mContext, R.string.reg_all_accept_plz);
			}
			break;
		}
	}

	private void setCountry(int position) {
		country.setText(countries[position]);
		countryNum.setText(countryNums[position]);
	}

	private String readAssetsFile(String fileName) {
		try {
			InputStream is = mContext.getAssets().open(fileName);

			int size = is.available();
			byte[] buffer = new byte[size];
			is.read(buffer);
			is.close();

			return new String(buffer);

		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}
}